﻿using Anke.SHManage.BLL;
using Anke.SHManage.Utility;
using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Utility;


namespace ShLeaderDeskWeb.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.AmbulanceDaiMing = AppConfig.GetConfigString("AmbulanceDaiMing");
            ViewBag.AmbulanceRenWu = AppConfig.GetConfigString("AmbulanceRenWu");
            ViewBag.AmbulanceZanTing = AppConfig.GetConfigString("AmbulanceZanTing");
            ViewBag.AmbulanceXiaBan = AppConfig.GetConfigString("AmbulanceXiaBan");
            ViewBag.GpsLastIntervalTime = AppConfig.GetConfigInt("GpsLastIntervalTime"); //获取配置文件里的车辆GPS查询定时间隔

            ViewBag.EnableLoadStation = AppConfig.GetConfigBool("EnableLoadStation"); //获取配置文件里的地图上是否显示分站标注
            ViewBag.EnableLoadHospital = AppConfig.GetConfigBool("EnableLoadHospital"); //获取配置文件里的地图上是否显示医院标注
            ViewBag.EnableLoadEvent = AppConfig.GetConfigBool("EnableLoadEvent"); //获取配置文件里的地图上是否显示事件标注

            ViewBag.MarkerClearIntervalTime = AppConfig.GetConfigInt("MarkerClearIntervalTime"); //add2021-01-17 plq 获取配置文件里的车辆轨迹、事件定位标注点清除定时间隔

            //add2021-04-15 plq 获取当前登录人的所属中心以及是否可切换当前中心 start
            string userCenterID = Convert.ToString(UserOperateContext.Current.Session_UsrInfo.ZhongXinBianMa); //当前登录用户的所属中心编码
            string userRoleID = Convert.ToString(UserOperateContext.Current.Session_UsrInfo.LeiXingBianMa); //当前登录用户的角色编码
            ViewBag.userCenterID = userCenterID;
            ViewBag.IsSwitchAble = false;
            string CenterSwitchableRoleCode = AppConfig.GetConfigString("CenterSwitchableRoleCode");
            List<string> roleList = new List<string>(CenterSwitchableRoleCode.Split(','));
            string CenterSwitchableCenterCode = AppConfig.GetConfigString("CenterSwitchableCenterCode");
            List<string> centerList = new List<string>(CenterSwitchableCenterCode.Split(','));

            if (roleList.Contains(userRoleID) && centerList.Contains(userCenterID)) //登录用户的中心编码 符合配置的所在中心，角色编码 符合配置的所属角色编码，则可切换
            {
                ViewBag.IsSwitchAble = true;
            }
            //add2021-04-15 plq 获取当前登录人的所属中心以及是否可切换当前中心 end
            return View();
        }

        #region 获取下拉列表数据
        //获取分中心
        [HttpPost]
        public ActionResult GetCenter()
        {
            try
            {
                var list = new M_DictionaryBLL().GetCenter();
                return Json(list);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }
        [HttpPost]
        //获取分站
        public ActionResult GetStation(string code)
        {
            try
            {
                if (code == "")
                { code = "-1"; }
                var list = new M_DictionaryBLL().GetStation(code);
                return Json(list);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }
        [HttpPost]
        //获取车辆分组
        public ActionResult GetAmbulanceGroup()
        {
            try
            {
                var list = new M_DictionaryBLL().GetAmbulanceGroup();
                return Json(list);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }
        [HttpPost]
        //获取车辆
        public ActionResult GetAmbulanceInfo()
        {
            try
            {
                var list = new M_DictionaryBLL().GetAmbulanceInfo();
                return Json(list);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }
        //获取所有调度员人员
        [HttpPost]
        public ActionResult GetDispatcher()
        {
            try
            {
                var list = new M_DictionaryBLL().GetDispatcher();
                return Json(list);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }

        //获取调度中心
        [HttpPost]
        public ActionResult GetDispatchCenter()
        {
            try
            {
                var list = new M_DictionaryBLL().GetDispatchCenter();
                return Json(list);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }

        //根据中心获取调度员人员
        public ActionResult GetDispatcherByCenter(string code)
        {
            try
            {
                if (code == "")
                { code = "-1"; }
                var list = new M_DictionaryBLL().GetDispatcherByCenter(code);
                return Json(list);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }

        #endregion

        //获取车辆列表
        [HttpPost]
        public ActionResult GetAmbulanceList()
        {
            int pageSize = int.Parse(Request.Form["rows"]);
            int pageIndex = int.Parse(Request.Form["page"]);

            //获取查询条件
            string CenterID = Convert.ToString(Request.Form["CenterID"]);
            string StationID = Convert.ToString(Request.Form["StationID"]);
            string GroupID = Convert.ToString(Request.Form["GroupID"]);
            string RealSign = Convert.ToString(Request.Form["RealSign"]);
            Boolean isDaiMing = Convert.ToBoolean(Request.Form["isDaiMing"]);
            Boolean isRenWu = Convert.ToBoolean(Request.Form["isRenWu"]);
            Boolean isZanTing = Convert.ToBoolean(Request.Form["isZanTing"]);
            Boolean isXiaBan = Convert.ToBoolean(Request.Form["isXiaBan"]);

            AmbulanceBLL bll = new AmbulanceBLL();
            var list = bll.GetAmbulanceList(pageSize, pageIndex, CenterID, StationID, GroupID, RealSign, isDaiMing, isRenWu, isZanTing, isXiaBan);
            return Json(list, "appliction/json", JsonRequestBehavior.AllowGet);

        }

        //获取当前事件呼救定位
        [HttpPost]
        public ActionResult GetXYInfoByTaskCode(string TaskCode)
        {
            AmbulanceBLL bll = new AmbulanceBLL();
            var list = bll.GetXYInfoByTaskCode(TaskCode);
            JsonResult j = this.Json(list, "appliction/json", JsonRequestBehavior.AllowGet);
            return j;
        }

        //获取历史呼救信息列表
        [HttpPost]
        public ActionResult GetTalarmEventList()
        {
            int pageSize = int.Parse(Request.Form["rows"]);
            int pageIndex = int.Parse(Request.Form["page"]);

            //获取查询条件
            DateTime StartTime = Convert.ToDateTime(Request.Form["start"]);
            DateTime EndTime = Convert.ToDateTime(Request.Form["end"]);
            string LocalAddress = Convert.ToString(Request.Form["localAddr"]);
            string SendAddress = Convert.ToString(Request.Form["sendAddr"]);
            string Tel = Convert.ToString(Request.Form["tel"]);
            string UserId = Convert.ToString(Request.Form["dispatcherID"]);
            string CenterID = Convert.ToString(Request.Form["CenterID"]); //add2021-01-20 plq 中心

            AmbulanceBLL bll = new AmbulanceBLL();
            var list = bll.GetTalarmEventList(pageSize, pageIndex, StartTime, EndTime, LocalAddress, SendAddress, Tel, UserId, CenterID);
            return Json(list, "appliction/json", JsonRequestBehavior.AllowGet);

        }

        #region 实时数据统计

        //获取实时数据信息
        //待派任务数
        [HttpPost]
        public ActionResult GetTalarmEventInfo()
        {
            try
            {
                string CenterID = Convert.ToString(Request.Form["CenterID"]);
                var list = new AmbulanceBLL().GetTalarmEventInfo(CenterID);
                return Json(list);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }

        //当班车辆数(车辆状态)
        [HttpPost]
        public ActionResult GetAmbulanceNum()
        {
            try
            {
                string CenterID = Convert.ToString(Request.Form["CenterID"]);
                var list = new AmbulanceBLL().GetAmbulanceNum(CenterID);
                return Json(list);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }

        /// <summary>
        /// 当班车辆数(车辆等级)
        /// add2020-12-25 
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetDutyTypeAmbNum()
        { 
            try
            {
                string CenterID = Convert.ToString(Request.Form["CenterID"]);
                var list = new AmbulanceBLL().GetDutyTypeAmbNum(CenterID);
                return Json(list);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }

        /// <summary>
        /// 途中待命车辆数(车辆等级)
        /// add2020-12-25 
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetWayStandByAmbNum()
        {
            try 
            {
                string CenterID = Convert.ToString(Request.Form["CenterID"]);
                var list = new AmbulanceBLL().GetWayStandByAmbNum(CenterID);
                return Json(list);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }

        /// <summary>
        /// 站内待命车辆数(车辆等级)
        /// add2020-12-25 
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetStationStandByAmbNum()
        {
            try
            {
                string CenterID = Convert.ToString(Request.Form["CenterID"]);
                var list = new AmbulanceBLL().GetStationStandByAmbNum(CenterID);
                return Json(list);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }

        /// <summary>
        /// 获取当前任务中的任务数据
        /// add2020-12-25 
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetCurrentTaskInfo() 
        {
            try
            {
                string CenterID = Convert.ToString(Request.Form["CenterID"]);
                var list = new AmbulanceBLL().GetCurrentTaskInfo(CenterID);
                return Json(list);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }

        #endregion


        #region 初始化调度数据--地图标注数据

        //初始化调度数据
        //获取车辆最后GPS
        [HttpPost]
        public ActionResult GetAmbLastGPS()
        {
            AmbulanceBLL bll = new AmbulanceBLL();
            string CenterID = Convert.ToString(Request.Form["CenterID"]);
            var list = bll.GetAmbLastGPS(CenterID);
            JsonResult j = this.Json(list, "appliction/json", JsonRequestBehavior.AllowGet);
            return j;
        }
        //获取 分站坐标信息
        [HttpPost]
        public ActionResult GetStationInfo()
        {
            AmbulanceBLL bll = new AmbulanceBLL();
            string CenterID = Convert.ToString(Request.Form["CenterID"]);
            var list = bll.GetStationInfo(CenterID);
            JsonResult j = this.Json(list, "appliction/json", JsonRequestBehavior.AllowGet);
            return j;
        }
        //获取 医院坐标信息
        [HttpPost]
        public ActionResult GetHospitalInfo()
        {
            AmbulanceBLL bll = new AmbulanceBLL();
            string CenterID = Convert.ToString(Request.Form["CenterID"]);
            var list = bll.GetHospitalInfo(CenterID);
            JsonResult j = this.Json(list, "appliction/json", JsonRequestBehavior.AllowGet);
            return j;
        }
        //获取 事件坐标信息
        [HttpPost]
        public ActionResult GetExcuteAlarmEventInfo()
        {
            AmbulanceBLL bll = new AmbulanceBLL();
            string CenterID = Convert.ToString(Request.Form["CenterID"]);
            var list = bll.GetExcuteAlarmEventInfo(CenterID);
            JsonResult j = this.Json(list, "appliction/json", JsonRequestBehavior.AllowGet);
            return j;
        }
        
        #endregion


        #region 轨迹回放查询页

        /// <summary>
        /// 车辆轨迹回放查询视图页---由于不同页面间地图方法调用问题，已弃用
        /// </summary>
        /// <param name="AmbCode">车辆编码</param>
        /// <param name="beginTime">查询开始时刻，形如"yyyy-MM-dd HH:mm:ss"</param>
        /// <param name="endTime">查看结束时刻，形如"yyyy-MM-dd HH:mm:ss"</param>
        /// <returns></returns>
        public ActionResult AmbGpsTrack(string AmbCode, string beginTime, string endTime)
        {
            ViewBag.AmbCode = AmbCode; //车辆编码
            DateTime now = DateTime.Now;
            if (string.IsNullOrEmpty(beginTime) && string.IsNullOrEmpty(endTime))
            {
                ViewBag.BeginTime = now.AddHours(-5).ToString("yyyy-MM-dd HH:mm:ss");
                ViewBag.EndTime = now.ToString("yyyy-MM-dd HH:mm:ss");
            }
            else
            {
                ViewBag.BeginTime = beginTime;
                ViewBag.EndTime = string.IsNullOrEmpty(endTime) ? now.ToString("yyyy-MM-dd HH:mm:ss") : endTime; //任务进行中完成时刻为空，此时以当前时刻作为查询截止时刻
            }
            return View();
        }

        //获取车辆GPS列表
        [HttpPost]
        public ActionResult GetGpsTrack(string AmbulanceCode, DateTime StartTime, DateTime EndTime, Boolean IsFilterStop)
        {
            AmbulanceBLL bll = new AmbulanceBLL();
            var list = bll.GetGpsTrack(AmbulanceCode, StartTime, EndTime, IsFilterStop);
            JsonResult j = this.Json(list, "appliction/json", JsonRequestBehavior.AllowGet);
            return j;
        }
        [HttpPost]

        //验证轨迹查询时间间隔
        public Object CheckTime(DateTime StartTime, DateTime EndTime)
        {
            TimeSpan ts = EndTime - StartTime;
            var MaxDay = AppConfig.GetConfigDouble("MaxGpsTrackInterval");
            if (ts.TotalDays > AppConfig.GetConfigDouble("MaxGpsTrackInterval"))
            {
                return Json(new { Result = false, Message = "最大查询天数为:" + MaxDay + "天,不能超过!" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { Result = true, Message = "时间查询条件通过!" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);
            }
        }
        #endregion

        #region 登录 add2021-04-15 plq

        /// <summary>
        /// 用户登录页
        /// </summary>
        /// <returns></returns>
        [SkipLoginAttribute]
        public ActionResult UserLoginPage()
        {
            return View();
        }

        /// <summary>
        /// Filters跳转到登录页面
        /// </summary>
        /// <returns></returns>
        [SkipLoginAttribute]
        public ActionResult RedirectToLogin()
        {
            return View();
        }

        /// <summary>
        /// 提交登录请求
        /// </summary>
        /// <param name="info">登录实体</param>
        /// <returns></returns>
        [HttpPost]
        [SkipLoginAttribute]
        public ActionResult UserLoginByInfo(LoginInfo info)
        {
            //首先检查用户名密码
            UserLoginBLL bll = new UserLoginBLL();
            if (bll.Login(info.UserName, info.PassWord))
            {
                //StateLog("登录", info.YongHuMing); //记录用户登录状态---暂时搁置
                //登录成功，跳转到主页面
                JsonResult jsr = this.JsonResult(E_JsonResult.OK, "", null, "~/Home/Index");
                jsr.ContentType = "text/html";
                return jsr;
            }
            else
            {
                //登录失败,发送错误信息给用户
                JsonResult jsr = this.JsonResult(E_JsonResult.Error, "登录失败！用户名或者密码错误！", null, null);
                jsr.ContentType = "text/html";
                return jsr;
            }
        }

        /// <summary>
        /// 退出登录
        /// </summary>
        /// <returns></returns>
        public ActionResult ExitLogin()
        {
            try
            {
                Session.Remove("UserInfo"); //清除用户信息session
                return Json("OK");
            }
            catch (Exception ex)
            {
                LogUtility.Error("Home/ExitLogin", ex.Message);
                return Json("NO");
            }
        }
        #endregion
    }
}